import cryptoJs from 'crypto-js';

// DES对称加密
/*
export const encryptDes = (message, key) => {
  const keyHex = cryptoJs.enc.Utf8.parse(key);
  const option = {mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7};
  const encrypted = cryptoJs.DES.encrypt(message, keyHex, option);
  return encrypted.ciphertext.toString();
}; */

/**
 * DES加密
 * @param {string} key 加密密钥
 * @param {string} val 需要加密的字符串
 * @returns 加密后的字符串
 */
export const encryptDes = (key, val) => {
  const keyHex = cryptoJs.enc.Utf8.parse(key)
  const option = {
    mode: cryptoJs.mode.ECB,
    padding: cryptoJs.pad.Pkcs7
  }
  const encrypted = cryptoJs.DES.encrypt(val, keyHex, option)
  return encrypted.toString()
}

/**
 * DES解密
 * @param {string} key 解密密钥
 * @param {string} val  加密后的字符串
 * @returns 解密后的字符串
 */
export const decryptDes = (key, val) => {
  const keyHex = cryptoJs.enc.Utf8.parse(key)
  const option = {
    mode: cryptoJs.mode.ECB,
    padding: cryptoJs.pad.Pkcs7
  }
  const decrypted = cryptoJs.DES.decrypt(val, keyHex, option)
  const decryptedStr = decrypted.toString(cryptoJs.enc.Utf8)
  return decryptedStr.toString()
}
